/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vng.survey.controller;


import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.AnswerItem;
import com.vng.zing.survey.thrift.model.QuesType;
import com.vng.zing.survey.thrift.model.SurveyItemResult;
import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static vng.survey.model.CommonModel.OFFSET;
import vng.survey.model.CommonModel;


/**
 *
 * @author namhh
 */
public class AjaxStatisticController extends BaseController {

//    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException, Exception {
//        String content="";
//        Integer page = Integer.parseInt(request.getParameter("page"));
//        page--;
//        String type = request.getParameter("type");
//        Short idQuestion = Short.parseShort(request.getParameter("idQuestion"));
//        Integer idSurvey = Integer.parseInt(request.getParameter("idSurvey"));
//        SurveyClient surveyClient = SurveyClient.getInstance();
//
//        TemplateDataDictionary dicAnswers = TemplateDictionary.create();
//        List<AnswerItem> answerItems = surveyClient.getResByQues(idSurvey, idQuestion, page, OFFSET);
//        if (type!=null&&type.equals(QuesType.MULTI_LINE.name())) {
//            
//            for (int i = 0; i < answerItems.size(); i++) {
//                TemplateDataDictionary sliceAnswer = dicAnswers.addSection("SLICE_ANSWER");
//                sliceAnswer.setVariable("NUMBER", String.format("%s", i + 1));
//                sliceAnswer.setVariable("USERNAME", String.format("USER %s", i + 1));
//                sliceAnswer.setVariable("CONTENT", answerItems.get(i).getAnswer());
//            }
//
////           
//             content = CommonModel.getStatMutilLine(dicAnswers);
//              
//        } else {
//
//
//            for (int i = 0; i < answerItems.size(); i++) {
//                TemplateDataDictionary sliceAnswer = dicAnswers.addSection("SLICE_ANSWER");
//                sliceAnswer.setVariable("NUMBER", String.format("%s", i + 1));
//                sliceAnswer.setVariable("USERNAME", String.format("USER %s", i + 1));
//                sliceAnswer.setVariable("CONTENT", answerItems.get(i).getAnswer());
//            }
//
////            for (int i = 0; i < page; i++) {
////                TemplateDataDictionary slicePages = dicAnswers.addSection("SLICE_PAGE");
////                slicePages.setVariable("PAGE_NUMBER", String.format("%s", i + 1));
////            }
//             content = CommonModel.getStatTable(dicAnswers);
//        }
//        response.setContentType("text/plain");
//        response.setCharacterEncoding("UTF-8");
//        response.getWriter().print(content);
//
//    }
    @Override
    public String render(HttpServletRequest request, HttpServletResponse resp) throws IOException, Exception {
        String content = "";
        Integer page = Integer.parseInt(request.getParameter("page"));
        page--;
        String type = request.getParameter("type");
        Short idQuestion = Short.parseShort(request.getParameter("idQuestion"));
        Integer idSurvey = Integer.parseInt(request.getParameter("idSurvey"));

      
        SurveyClient surveyClient = SurveyClient.getInstance();
        SurveyItemResult itemResult = surveyClient.getSurvey(idSurvey);
        if (itemResult.getErro() != com.vng.zing.survey.thrift.model.Error.SUCCESS || itemResult.getSurveyItem().getIDUser() != 1) {
            return content;
        }
        if (itemResult.getSurveyItem().isOption() == false ) {
            return content;
        }
        TemplateDataDictionary dicAnswers = TemplateDictionary.create();
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date date;
        List<AnswerItem> answerItems = surveyClient.getResByQues(idSurvey, idQuestion, page, OFFSET);
        if (type != null && type.equals(QuesType.MULTI_LINE.name())) {

            for (int i = 0; i < answerItems.size(); i++) {
                TemplateDataDictionary sliceAnswer = dicAnswers.addSection("SLICE_ANSWER");
                sliceAnswer.setVariable("NUMBER", String.format("%s", page * OFFSET + i + 1));
                sliceAnswer.setVariable("USERNAME", String.format("USER %s", answerItems.get(i).getDisplayName()));
                sliceAnswer.setVariable("CONTENT", answerItems.get(i).getAnswer());
                date = new Date(((long) answerItems.get(i).getTimeResponse()) * 1000L);
                sliceAnswer.setVariable("TIMERESPONSE", df.format(date));
            }

//           
            content = CommonModel.getStatMutilLine(dicAnswers);

        } else {

            for (int i = 0; i < answerItems.size(); i++) {
                TemplateDataDictionary sliceAnswer = dicAnswers.addSection("SLICE_ANSWER");
                sliceAnswer.setVariable("NUMBER", String.format("%s", page * OFFSET + i + 1));
                sliceAnswer.setVariable("USERNAME", String.format("USER %s", answerItems.get(i).getDisplayName()));
                sliceAnswer.setVariable("CONTENT", answerItems.get(i).getAnswer());
                date = new Date(((long) answerItems.get(i).getTimeResponse()) * 1000L);
                sliceAnswer.setVariable("TIMERESPONSE", df.format(date));
            }

//            for (int i = 0; i < page; i++) {
//                TemplateDataDictionary slicePages = dicAnswers.addSection("SLICE_PAGE");
//                slicePages.setVariable("PAGE_NUMBER", String.format("%s", i + 1));
//            }
            content = CommonModel.getStatTable(dicAnswers);
        }
        return content;
    }

}
